<?php
namespace app\manage\validate;
use think\Validate;
class ShuomingValidate extends Validate
{
     protected $rule = [
        'title' =>  'require|lizhiliUnique:shuoming',
        'text' =>  'require',
		'url'=>'urlPro'
    ];
	 protected $message  =   [
        'title.require' => '文章名称必须填写',
        'title.lizhiliUnique'     => '当前文章名称已经存在，不能重复！',
        'text.require'   => '文章内容必须选择',
		'url.urlPro'   => '跳转网站格式不正确',
    ];
	protected $scene = [
        'edit'  =>  [
            'title' =>  'require',
            'text' =>  'require',
            'url'=>'urlPro'
        ],
    ];
    //自定义url格式验证
    protected function urlPro($value, $rule, $data=[],$field='',$desc='')
    {
        if($value==''){
            return true;
        }
        if(strpos($value,'http://')===false && strpos($value,'https://')===false){
            return false;
        }
        return true;
    }
    // 自定义验证规则
    protected function lizhiliUnique($value, $rule, $data=[],$field='',$desc='')
    {
        $where = [['delete_time','=',Null],[$field,'=',$value]];
        $str='\\app\\model\\'.ucfirst($rule).'Model';
        $count = (new $str())->where($where)->count();
        if($count){
            return false;
        }
        return true;
    }
}
